package pens.penObjects
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import pens.PenType;
	import pens.interfaces.IPenPlayer;

	/**
	 * 鼠标毛笔 
	 * @author Administrator
	 * 
	 */
	public class Pen_Brush_Mouse extends Sprite implements IPenPlayer
	{
		private static var instance:Pen_Brush_Mouse;
		/**
		 *单例模式
		 *
		 **/
		public static function getInstance():Pen_Brush_Mouse{
			instance||=new Pen_Brush_Mouse()
			return instance;
		}
		/***************************************************/
		

		private var _isDown:Boolean=false	
		public function get penName():String{return _penName}
		
		public function draw(w:Number,h:Number):void{
			_pen.width=w
			_pen.height=h
			_pen.resign()
		}
	  
		public function resign():void
		{
			_pen.resign()
		}
		
	
		
		public function moveTo(x:Number, y:Number, size:Number=10, color:uint=0):void
		{
			_pen.moveTo(x,y,size,color)	
		}
		
		public function lineTo(x:Number, y:Number, size:Number=10, color:uint=0):void
		{
			_pen.lineTo(x,y,size,color)
		}
		
		public function end():void
		{
			_pen.end()
		}
		
		public function dispose():void
		{
		}
		
		
		private var _penName:String
		private var _pen:IPenPlayer
		
	    public function get penDates():Vector.<PenDate>{
			if(_pen==null)return null
			return _pen.penDates}
		
		public function Pen_Brush_Mouse()
		{
			super();
			_penName=PenType.PEN_BRUSH_MOUSE
			if (stage) inits();
			else addEventListener(Event.ADDED_TO_STAGE, inits);
			
			_pen=new Pen_Brush()
			_pen.resign()
			addChild(_pen as ActionBitmap)
		}
		
		
		private function inits(e:Event=null):void{
			removeEventListener(Event.ADDED_TO_STAGE, inits);
			
			
			addEventListener(MouseEvent.MOUSE_DOWN,ondownHand)
			stage.addEventListener(MouseEvent.MOUSE_UP,onupHand);
			addEventListener(MouseEvent.MOUSE_MOVE, onmoveHand);

		}
		
		
		private function ondownHand(e:MouseEvent):void{
			_isDown=true
			moveTo(mouseX,mouseY,PenType.penSize,PenType.penColor)	
			lineTo(mouseX+1,mouseY+1,PenType.penSize,PenType.penColor)	
		}
		
		private function onupHand(e:MouseEvent):void{
		   _isDown=false
		}
		
		
		private function onmoveHand(e:MouseEvent):void{
			if(!_isDown)return
			lineTo(mouseX,mouseY,PenType.penSize,PenType.penColor)
		}
	}
}